using System;
using System.IO;
using System.Windows.Forms;
using DZTC.UselessAppEver.Core;

namespace DZTC.UselessAppEver.UI
{
    public class SelectFilesController
    {
        private SelectFilesModel Model { get; set; }
        private SelectFilesForm View { get; set; }

        private bool CheckInputFile(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                Dialog.PromptInputFileNotSet();
                return false;
            }

            if (File.Exists(fileName))
            {
                return true;
            }
            else
            {
                Dialog.PromptInputFileNotFound(fileName);
                return false;
            }
        }


        private bool CheckOutputFile(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                Dialog.PromtOutputFileNotSet();
                return false;
            }

            if (!FileHelpers.CheckFileNameCorrectness(fileName))
            {
                Dialog.PromtOutputFileNameInvalid(fileName);
                return false;
            }

            if (File.Exists(fileName) && !Dialog.PromptOutputFileAlreadyExist(fileName))
            {
                return false;
            }

            var directory = Path.GetDirectoryName(fileName);
            if (!Directory.Exists(directory) && !Dialog.PromtOuptutFileDirectoryNotExist(directory))
            {
                return false;
            }

            return true;

        }

        public void SetInputFileName(string fileName)
        {
            if (fileName.EqualsAsFileName(Model.OutputFile))
            {
                Dialog.PromptInputAndOutputFilesAreSame();
                return;
            }

            Model.InputFile = fileName;
            View.SetInputFile(fileName);
        }

        public void SetOutputFileName(string fileName)
        {
            if (fileName.EqualsAsFileName(Model.InputFile))
            {
                Dialog.PromptInputAndOutputFilesAreSame();
                return;
            }

            View.SetOutputFile(fileName);
            Model.OutputFile = fileName;
        }


        public void ChangeDirection(ConversionDirection newDirection)
        {
            if (newDirection == ConversionDirection.Decode &&
                !Path.GetExtension(Model.InputFile).IgnoreCaseEquals(Const.FILE_EXTENSION))
                SetInputFileName(null);

            if (newDirection == ConversionDirection.Encode &&
                !Path.GetExtension(Model.OutputFile).IgnoreCaseEquals(Const.FILE_EXTENSION))
                SetOutputFileName(null);

            View.SetDirection(newDirection);
            Model.Direction = newDirection;
        }


        private static string RemoveLastExtension(string fileName)
        {
            return Path.Combine(Path.GetDirectoryName(fileName),
                                Path.GetFileNameWithoutExtension(fileName));
        }

        private SelectFilesModel ShowInternal(SelectFilesModel model)
        {
            Model = model;

            View.SetInputFile(model.InputFile);
            View.SetOutputFile(model.OutputFile);
            View.SetDirection(model.Direction);

            return View.ShowDialog() == DialogResult.OK ? Model : null;
        }

        public  SelectFilesModel ShowDefault()
        {
            View.SetDisplayingMode(DisplayingMode.Default);

            var model = new SelectFilesModel
                {
                    Direction = ConversionDirection.Encode
                };

            return ShowInternal(model);
        }

      

        public void Start()
        {
            if (CheckInputFile(Model.InputFile) && CheckOutputFile(Model.OutputFile))
            {
                View.CloseOk();
            }
        }

        public SelectFilesModel ShowEncode(string fileName)
        {
            View.SetDisplayingMode(DisplayingMode.Encode);

            var model = new SelectFilesModel
                {
                    Direction = ConversionDirection.Encode,
                    InputFile = fileName,
                    OutputFile = fileName + Const.FILE_EXTENSION
                };

            return ShowInternal(model);
        }

        
        public  SelectFilesModel ShowDecode(string fileName)
        {
            View.SetDisplayingMode(DisplayingMode.Decode);

            String outputFile = null;
            if (Path.GetExtension(fileName) == Const.FILE_EXTENSION)
            {
                outputFile = RemoveLastExtension(fileName);
            }

            var model = new SelectFilesModel
                {
                    Direction = ConversionDirection.Decode,
                    InputFile = fileName,
                    OutputFile = outputFile
                };

            return ShowInternal(model);
        }


        public SelectFilesController()
        {
            View = new SelectFilesForm(this);
        }
    }
}